class TeachersController < ApplicationController
  def index
    @teachers = Teacher.all
    p @teacher
  end

  def register
    @courses = Course.all.collect{|x| [x.name, x.id]}
    if params[:teacher]
      @teacher = Teacher.new(params[:teacher])
      @teacher.password = generate_pwd
      @teacher.save
      redirect_to :action => 'index', :notice => "教师登记成功"
    end
  end

  def edit
    @teacher = Teacher.find(params[:id])
    @courses = Course.all.collect{|x| [x.name,x.id]}
  end

  def update
    @teacher = Teacher.find(params[:id])
    if @teacher.update_attributes(params[:teacher])
      redirect_to :action => "index", :notice => "教师信息更新成功"
    else
      render :action => "edit"
    end
  end

  def destroy
    @user = Teacher.find(params[:id])
    @user.destroy
    redirect_to :action => "index"
  end

  def generate_pwd
    return "12345"
  end
end
